﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormCircleTool : Form
    {
        HObject m_Image;
        SetCircle m_SetCircle;
        public FormCircleTool(HObject _Image, SetCircle _SetCircle)
        {
            try
            {
                InitializeComponent();
                HOperatorSet.GenEmptyObj(out m_Image);
                m_Image = _Image;
                m_SetCircle = _SetCircle;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
         
        }

        private void btn_DrawCircle_Click(object sender, EventArgs e)
        {
            try
            {
                btn_DrawCircle.Enabled = false;
                btn_Clear.Enabled = false;
                button2.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                hWindowControl1.Focus();
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 20, 20, 15, "green");
                HOperatorSet.DrawCircle(hWindowControl1.HalconWindow, out HTuple CenterY, out HTuple CenterX, out HTuple Radius);
                m_SetCircle.Rows[0] = CenterY - Radius;
                m_SetCircle.Cols[0] = CenterX;
                m_SetCircle.Rows[1] = CenterY;
                m_SetCircle.Cols[1] = CenterX + Radius;
                m_SetCircle.Rows[2] = CenterY + Radius;
                m_SetCircle.Cols[2] = CenterX;
                m_SetCircle.Rows[3] = CenterY;
                m_SetCircle.Cols[3] = CenterX - Radius;
                m_SetCircle.Rows[4] = CenterY - Radius;
                m_SetCircle.Cols[4] = CenterX;
                HOperatorSet.GenCircle(out HObject circle, CenterY, CenterX, Radius);
                HOperatorSet.DispObj(circle, hWindowControl1.HalconWindow);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 20, 20, 15, "green");
                btn_DrawCircle.Enabled = true;
                btn_Clear.Enabled = true;
                button2.Enabled = true;
                btn_Refresh.Enabled = true;
                btn_SaveParameter.Enabled = true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }

        private void FormCircleTool_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text += "图像为空，请加载图像！";
                }
                else
                {
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.GetImageSize(m_Image, out Width, out Height);
                    HOperatorSet.SetSystem("width", Width);
                    HOperatorSet.SetSystem("height", Height);
                    MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                    m_Image.DispObj(hWindowControl1.HalconWindow);
                    tool_ResultInfo.Text = "图像加载成功。";
                }
                if (m_SetCircle != null)
                {
                    m_SetCircle.ReadParameter();
                    this.propertyGrid1.SelectedObject = m_SetCircle;
                    this.propertyGrid1.Refresh();
                }
                else
                {
                    tool_ResultInfo.Text += "线段工具模块对象不存在。";
                }
            }
            catch
            {

            }
      
        }

        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            MyHOperatorSet.ZoomImage(hWindowControl1, m_Image, e);
        }

        private void 适应图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
        }

        private void 保存原图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveImage(m_Image);
        }

        private void 保存窗口图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveWindowDump(hWindowControl1.HalconWindow);
        }


        private void btn_Clear_Click(object sender, EventArgs e)
        {
            HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
            HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
            m_SetCircle.Cols = new double[5];
            m_SetCircle.Rows = new double[5];
            //HOperatorSet.GenEmptyObj(out m_Lines.DrawLineRegion);
            //HOperatorSet.CountObj(m_Lines.DrawLineRegion, out HTuple tuple1);
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            if (m_SetCircle != null)
            {
                m_SetCircle.ReadParameter();
                this.propertyGrid1.SelectedObject = m_SetCircle;
                this.propertyGrid1.Refresh();
                tool_ResultInfo.Text = "刷新成功";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空，请加载图像！";
                    MessageBox.Show("图像为空，请加载图像！");
                    return;
                }


                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                if (m_SetCircle.Rows[1] > 0 && m_SetCircle.Cols[1] > 0)
                {
                    m_SetCircle.Fit_Circle(m_Image, hWindowControl1.HalconWindow, m_SetCircle.Rows, m_SetCircle.Cols);
                    if (m_SetCircle.RowCenter.D > 0 && m_SetCircle.ColCenter.D > 0)
                    {
                        this.tool_ResultInfo.Text = $"拟合圆成功  圆心X:{m_SetCircle.ColCenter} Y:{m_SetCircle.RowCenter} 半径R:{m_SetCircle.RadiusCircle}";
                    }
                }
                else
                {
                    MessageBox.Show("绘制圆坐标为空,清先绘制圆");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {
            try
            {
                m_SetCircle.WriteParameter();
                MessageBox.Show("参数保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

      
    }
}
